/**
 * 上下文用户/系统当前用户
 * @author Canaan
 * @date 2019/3/7 11:16
 */
export default {

    state: {
        userContext: {
            id: null,           //主键
            nickname: '',       //呢称
            username: sessionStorage.getItem('x_asia_tasty_login_username'), //账号
            role: sessionStorage.getItem('x_asia_tasty_login_role'),     //角色
        }
    },

    getters: {},

    mutations: {
        userContext({userContext}, loginUser) {
            userContext = Object.assign(userContext, {
                id: null,
                nickname: null,
                username: null,
                role: null,
            }, loginUser);
        }
    },

    actions: {
        loadUserContext({commit, state}, vue) {
            //sessionStorage 没有token 那么不加载
            const token = sessionStorage.getItem(vue.$env.LOGIN_TOKEN);
            if (!token) {
                return;
            }

            return vue.$ajax.get('auth/current_user')
                .then(res => {
                    debugger
                    if (res.code !== 0) {
                        throw res.msg;
                    }
                    commit('userContext', res.data);
                    return res.data;
                });
        }
    }

}